package com.flying.common;

import java.util.List;

import com.flying.common.util.ServiceHelper;
import com.flying.common.util.Utils;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.data.Data;
import com.flying.framework.service.AbstractService;

@Service("props")
public class PropertyService extends AbstractService {
	@MethodInfo("查询")
	public Data find(
			@Param(value="v", required=true,	desc="view") String view,
			@Param(value="m", required=true,	desc="view") String module,
			@Param(value="s", required=true,	desc="view") String service,
			@Param(value="id", required=true,	desc="view") String id
		) {
		Data formData = ServiceHelper.invoke("pas", "FormService:findByModuleAndCode", new Data("module", module,"code", view));
		Long formId = formData.getLong("formId");
		List<Data> fields = ServiceHelper.invoke("pas", "FieldService:findByFormId", new Data("form_id", formId)).get("rows");
		//
		String idField = formData.getString("pkFields");
		var request = new Data();
		request.put(idField, id);
		Data vo = null;
		try {
			vo = ServiceHelper.invoke(module, service, request);
		} catch (Exception e) {
			vo = new Data();
		}
		//
		List<Prop> rows = Utils.newArrayList();
		for(Data f: fields) {
			String title = f.getString("title");
			String name = f.getString("name");
			Object value = vo.get(name);
			rows.add(new Prop(title, value));
		}
		return new Data("total", rows.size(), "rows", rows);
	}
	
	@SuppressWarnings("serial")
	public class Prop extends Data{
		private String name, group, editor;
		private Object value;
		
		public Prop(String name, Object value) {
			this.name = name;
			this.value = value;
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getGroup() {
			return group;
		}
		public void setGroup(String group) {
			this.group = group;
		}
		public String getEditor() {
			return editor;
		}
		public void setEditor(String editor) {
			this.editor = editor;
		}
		public Object getValue() {
			return value;
		}
		public void setValue(Object value) {
			this.value = value;
		}
	}
}
